package com.hairline.shiro;


import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

//用来维护跨域的session
public class ShiroSessionManager extends DefaultWebSessionManager {
    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String sessionId = httpServletRequest.getHeader("X-cskaoyan-mall-Admin-Token");
        if (!StringUtils.isEmpty(sessionId)) {
            return sessionId;
        }

        String sessionId2 = httpServletRequest.getHeader("X-Litemall-Token");
        if (!StringUtils.isEmpty(sessionId2)) {
            return sessionId2;
        }

        //不存在就返回自带的SessionId
        return super.getSessionId(request, response);
    }
}
